package com.hrt;

import com.hrt.mapper.DeptMapper;
import com.hrt.pojo.Dept;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Auther: hrt
 * @Date: 2021/10/14
 * 使用Mapper代理的方式
 */
public class DeptMapperProxyTest {


    private SqlSession sqlSession;
    @Before
    public void init(){
        SqlSessionFactoryBuilder ssfb =new SqlSessionFactoryBuilder();
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory factory=ssfb.build(resourceAsStream) ;
        sqlSession=factory.openSession();
    }
    @Test
    public void testFindAll(){
        DeptMapper deptMapper=sqlSession.getMapper(DeptMapper.class);
        List<Dept> depts = deptMapper.findAll();
        depts.forEach(System.out::println);
    }
    @After
    public void release(){
        // 关闭SQLSession
        sqlSession.close();
    }
}
